package com.itheima;

public class Demo5 {

    public static void main(String[] args) {
        int [] a = {12, 20, 5, 16, 15, 1, 30, 45, 23, 9, 4, 14};

        int min = 0;
        int max = a.length-1;
        sort(a,min,max);
        for (int i : a) {
            System.out.println("i = " + i);
        }
    }



    public static void sort(int [] a,int min,int max){
        int key = a[min];
        int start = min;
        int end = max;
        while (end > start) {
            while (end > start && a[end] >= key) {
                end--;
            }
            if (a[end] < key) {
                int i = a[start];
                a[start] = a[end];
                a[end] = i;
            }

            while (end > start && a[start] <= key) {
                start++;
            }
            if (a[start] > key) {
                int i = a[start];
                a[start] = a[end];
                a[end] = i;
            }
        }

        if (start > min) {
            sort(a,min,start-1);
        }
        if (end < max){
            sort(a,end+1,max);
        }
    }
}
